//
// IniFileConfiguration.cpp
//
// $Id: //poco/1.4/Util/src/IniFileConfiguration.cpp#1 $
//
// Library: Util
// Package: Configuration
// Module:  IniFileConfiguration
//
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier:	BSL-1.0
//


#include "Poco/Util/IniFileConfiguration.h"


#ifndef POCO_UTIL_NO_INIFILECONFIGURATION


#include "Poco/Exception.h"
#include "Poco/String.h"
#include "Poco/Path.h"
#include <set>
#include <fstream>


using Poco::icompare;
using Poco::trim;
using Poco::Path;


namespace Poco {
namespace Util {


IniFileConfiguration::IniFileConfiguration()
{
}


IniFileConfiguration::IniFileConfiguration(std::istream& istr)
{
	load(istr);
}

	
IniFileConfiguration::IniFileConfiguration(const std::string& path)
{
	load(path);
}


IniFileConfiguration::~IniFileConfiguration()
{
}


void IniFileConfiguration::tripchar(std::string& src, char delc)
{
    std:: string tmp;
    int i = 0;
    while( i < src.length())
    {
        if(src[i] == delc)
        {
            ++i;
            continue;
        }
        tmp += src[i];
        ++i;
    }
    src = tmp;
}


void IniFileConfiguration::load(std::istream& istr)
{
	m_map.clear();
	const int linelen 		= 256;
	char    buf[linelen]	={'\0'};
	std::string     session;
	while(!istr.eof())
	{
		memset(buf,'\0',linelen);
		istr.getline(buf,linelen);
		std::string line(buf);
		line = trim(line);
		if(line.empty())
			continue;
		//注释处理
		//std::replace(line.begin(),line.end(),' ','');
		tripchar(line,' ');
		tripchar(line,'\t');
		tripchar(line,'\n');
		tripchar(line,'\r');
		if(line[0] == '#' || (line[0] == '/' && line[1] == '/'))
			continue;
		
		//具体配置处理
		if(line[0] == '[')
		{
			session = line.substr(1,line.length() - 1);
			int pos = session.find("]");
			session = session.substr(0,pos);
			tripchar(session,' ');
			tripchar(session,'\t');
			continue;
		}
		else
		{
			std::string     key,value;
			int pos         = line.find("=");
			key				= line.substr(0,pos);
			value           =line.substr(pos+1);
			int notepos    = value.find("#");
			if(notepos != std::string::npos)
				value       = value.substr(0,notepos);
			notepos        = value.find("//");
			if(notepos != std::string::npos)
				value       = value.substr(0,notepos);
			notepos        = value.find(";");
			if(notepos != std::string::npos)
				value       = value.substr(0,notepos);
			
			value			= trim(value);
			if (value.size() > 2 && value[0]=='\"' && value[value.size()-1] == '\"')
				value = value.substr(1,value.size()-2);
		
			std::string fullKey = session;
			if (!fullKey.empty()) fullKey += '.';
			fullKey.append(trim(key));
			m_map[fullKey] = value;
		}
	}  
}
	

void IniFileConfiguration::load(const std::string& path)
{
	std::fstream    fs(path.c_str());
	load(fs);
	fs.close();
}


bool IniFileConfiguration::getRaw(const std::string& key, std::string& value) const
{
	std::map<std::string,std::string>::const_iterator it = m_map.find(key);
	if (it != m_map.end())
	{
		value = it->second;
		return true;
	}
	else return false;
}


void IniFileConfiguration::setRaw(const std::string& key, const std::string& value)
{
	m_map[key] = value;
}


void IniFileConfiguration::enumerate(const std::string& key, Keys& range) const
{
	std::set<std::string> keys;
	std::string prefix = key;
	if (!prefix.empty()) prefix += '.';
	std::string::size_type psize = prefix.size();
	for (std::map<std::string,std::string>::const_iterator it = m_map.begin(); it != m_map.end(); ++it)
	{
		if (icompare(it->first, psize, prefix) == 0)
		{
			std::string subKey;
			std::string::size_type end = it->first.find('.', psize);
			if (end == std::string::npos)
				subKey = it->first.substr(psize);
			else
				subKey = it->first.substr(psize, end - psize);
			if (keys.find(subKey) == keys.end())
			{
				range.push_back(subKey);
				keys.insert(subKey);
			}
		}
	}
}


void IniFileConfiguration::removeRaw(const std::string& key)
{
	std::string prefix = key;
	if (!prefix.empty()) prefix += '.';
	std::string::size_type psize = prefix.size();
	std::map<std::string,std::string>::iterator it = m_map.begin();
	std::map<std::string,std::string>::iterator itCur;
	while (it != m_map.end())
	{
		itCur = it++;
		if ((icompare(itCur->first, key) == 0) || (icompare(itCur->first, psize, prefix) == 0))
		{
			m_map.erase(itCur);
		}
	}
}


} } // namespace Poco::Util


#endif // POCO_UTIL_NO_INIFILECONFIGURATION